package com.liujit.standard.basis.core.result;


import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.liujit.standard.basis.core.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @description: 接口默认返回结果
 * @author: liujun
 * @create: 2020/12/31 4:47 下午
 **/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@JsonPropertyOrder({"code", "msg", "data"})
public class Result implements Serializable {

    private static final long serialVersionUID = 8688939572908756965L;

    private Integer code;

    private String msg;

    private Object data;

    public static Result success(Object data) {
        Result result = new Result();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result failure(ResultCode resultCode) {
        Result result = new Result();
        result.setResultCode(resultCode);
        return result;
    }

    public static Result failure(String message) {
        Result result = new Result();
        result.setCode(ResultCode.FAILURE.code());
        result.setMsg(message);
        return result;
    }

    public static Result failure(Integer code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    private void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.msg();
    }
}
